class GroupsController < ApplicationController
  before_filter :set_session
  before_filter :login_required, :except => [:show, :index]
  before_filter :unread_messages_amount, :only => [:new, :edit, :show, :invite_moderator, :index]
  
  def new
    flash[:notice] = "只有管理员才能创建" unless current_user.is_admin?
    @group = Group.new
  end
  
  def create
    @group = Group.new(params[:group])
    @group.creator = current_user
    if logged_in? && current_user.is_admin?
      flash[:notice] = "创建了#{@group.title}" if @group.save!
    else
      flash[:notice] = "只有管理员才能创建"
    end
    redirect_to :controller => '/admin/general', :action => 'group_manage'
  rescue ActiveRecord::InvalidRecord
    render :action => 'new'
  end
  
  def edit
    @group = Group.find(params[:id])
  end
  
  def update
    @group = Group.find(params[:id])
    @group.update_attributes!(params[:group])
    flash[:notice] = "你修改了#{@group.title}的资料"
    redirect_back_or_default(group_url(@group.id))
  end
  
  def show
    @group = Group.find(params[:id])
    
    if @group.hidden?
      flash[:notice] = "你访问的讨论区已经关闭"
      redirect_to groups_url
    end
    
    @topics = @group.topics.paginate(:all, 
                                     :include => [:user, :replied_by_user],
                                     :order => "topics.updated_at desc",
                                     :page => params[:page] || 1)
    
    @moderators = @group.moderators
    @other_topics = Topic.find(:all, :limit => 10, 
                                     :conditions => ["groups.hidden<>? and topics.group_id<>?", true, @group.id],
                                     :order => "topics.updated_at desc", 
                                     :include => [:replied_by_user, :user, :group])
    #@members    = @group.members - @moderators
    #store_location
  end
  
  def join
    @group = Group.find(params[:id])
    if GroupMembership.count("id", :conditions => ["group_id=? and user_id=?", @group.id, current_user.id])>0
      flash[:notice] = "你已经是#{@group.title}小组的成员了"
    else
      @membership = GroupMembership.create(:group_id => @group.id, :user_id => current_user.id)
      flash[:notice] = "你已经加入#{@group.title}小组"
    end
    redirect_back_or_default(group_url(@group))
  end
  
  def quit
    @group = Group.find(params[:id])
    @membership = GroupMembership.find(:first, :conditions => ["group_id=? and user_id=?", @group.id, current_user.id])
    @membership.destroy
    flash[:notice] = "你已经退出了#{@group.title}小组"
    redirect_back_or_default(group_url(@group))
  end
  
  def invite_moderator
    @group = Group.find(params[:id])
    @members = @group.members - @group.moderators
  end
  
  def add_moderator
    @group = Group.find(params[:id])
    @members = @group.members - @group.moderators
    0.upto(@members.size-1) do |i|
      id = params[:moderator]["no_#{i}"].to_i
      if id != 0
        # it is a user id
        moderator = User.find(id)
        Moderatorship.create(:group_id => @group.id, :user_id => id)
        msg = Message.new
        msg.sender_id = 0
        msg.receiver_id = id
        msg.title = "你被#{current_user.login}设为#{@group.title}小组的管理员了"
        msg.content = "#{moderator.login}，\r\n\r\n" +
                      "#{current_user.login}(#{user_url(current_user.id)})将你设为#{@group.title}小组(#{group_url(@group.id)})的管理员了\r\n\r\n\r\n\r\n\r\n\r\n" +
                      "多背一公斤客服"
        msg.save!
      end
    end
    flash[:notice] = "成功添加了管理员"
    redirect_back_or_default(group_url(@group.id))
  end
  
  def index
    @topics = Topic.find(:all, :limit => 30, 
                               :conditions => ["groups.hidden<>?", true],
                               :order => "topics.updated_at desc", 
                               :include => [:replied_by_user, :user, :group])
    @boards = Group.find(:all, :conditions => ["groups.hidden<>?", true], :order => "groups.position asc")
  end
  
  def hidden
    @group = Group.find(params[:id])
    @group.hidden = (@group.hidden? ? false : true)
    @group.save!
    flash[:notice] = "你隐藏了 #{@group.title}"
    redirect_to :controller => '/admin/general', :action => 'group_manage'
  end
  
  def edit_position
    @group = Group.find(params[:id])
  end
  
  def update_position
    @group = Group.find(params[:id])
    @group.update_attributes!(params[:group])
    redirect_to :controller => '/admin/general', :action => 'group_manage'
  end
  
  protected
=begin
  def authorized?
    logged_in? && (current_user.is_admin? || current_user
  end
=end  
  def set_session
    session[:nav] = "groups"
  end
end
